package com.cqjtu.tto.handler;

import cn.dev33.satoken.stp.StpInterface;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqjtu.tto.dao.mapper.*;
import com.cqjtu.tto.dao.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义权限验证接口扩展 
 */
@Component    // 保证此类被SpringBoot扫描，完成sa-token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionsRoleMapper permissionsRoleMapper;

    @Autowired
    private PermissionsMapper permissionsMapper;
    /**
     * 返回一个账号所拥有的权限码集合 
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginKey) {
        List<String> list = new ArrayList<String> ();
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysUserRole> sysUserRoleWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<PermissionsRole> permissionsRoleWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
        System.out.println("============");
        System.out.println(loginId.toString());
        System.out.println("============");
        sysUserWrapper.eq(SysUser::getUserName,loginId);
        sysUserWrapper.last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        sysUserRoleWrapper.eq(SysUserRole::getUserId,sysUser.getUserId());
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(sysUserRoleWrapper);
        for (SysUserRole sysUserRole : sysUserRoles) {
            roleWrapper.eq(Role::getRoleId,sysUserRole.getRoleId());
            roleWrapper.last("limit 1");
            Role role = roleMapper.selectOne(roleWrapper);
            permissionsRoleWrapper.eq(PermissionsRole::getRoleId,role.getRoleId());
            List<PermissionsRole> permissionsRoles = permissionsRoleMapper.selectList(permissionsRoleWrapper);
            for (PermissionsRole permissionsRole : permissionsRoles) {
                permissionsWrapper.eq(Permissions::getPermissionsId,permissionsRole.getPermissionsId());
                permissionsWrapper.last("limit 1");
                Permissions permissions = permissionsMapper.selectOne(permissionsWrapper);
                list.add(permissions.getPermissionsName());
            }
        }
        return list;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginKey) {

        List<String> list = new ArrayList<String>();
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysUserRole> sysUserRoleWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        System.out.println(loginId.toString());
        sysUserWrapper.eq(SysUser::getUserName,loginId);
        sysUserWrapper.last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        sysUserRoleWrapper.eq(SysUserRole::getUserId,sysUser.getUserId());
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(sysUserRoleWrapper);
        for (SysUserRole sysUserRole : sysUserRoles) {
            roleWrapper.eq(Role::getRoleId,sysUserRole.getRoleId());
            roleWrapper.last("limit 1");
            Role role = roleMapper.selectOne(roleWrapper);
            list.add(role.getRoleName());
        }
        return list;
    }

}